// AbstractGauge.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "AbstractGauge.h"
#include "..\Infobus\Infobus.h"
#include "..\Infobus\Types.h"
#include "..\Infobus\XmlHelper.h"
#include "..\Infobus\DatabaseAdapter.h"
#include "..\Infobus\EventHandler.h"
#include "..\Infobus\ActionHandler.h"
#include <sstream>

#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
    return TRUE;
}

#ifdef _MANAGED
#pragma managed(pop)
#endif

AbstractGauge::AbstractGauge()
: IListener(EVENTS)
, pDisp(NULL)
{
  this->setAliveBeats(true);
  pDisp = new Dispatcher(ACTIONS);
  pDisp->setAliveBeats(false);
}

AbstractGauge::~AbstractGauge()
{
  if (pDisp) delete pDisp;
}

void AbstractGauge::onMessage(const char* m)
{
  if (m) {
    long lMsgId = 0;   
    EventScheme * pEventScheme = NULL;
    Action * pAction = NULL;
    EventHandler * pEvthnd = new EventHandler;
    ActionHandler * pActhnd = new ActionHandler;

    pEvthnd->SelectMsgId(m, &lMsgId);
    pEvthnd->EventToEventScheme(m, &pEventScheme);    
    if (pEventScheme && pEventScheme->isValid()) {
      pEvthnd->SetEventSchemeRefMsgID(lMsgId, pEventScheme);        
      pEvthnd->SelectFullPattern(&pEventScheme);
      pEvthnd->SelectFullTrace(&pEventScheme);
    }

    pActhnd->SearchActionMetaData(m, &pAction);
    if (pAction) {
      if (pEventScheme && pEventScheme->isValid()) 
        pAction->addEvent(pEventScheme);

      if (pAction->isValid()) pDisp->sendMessage(pAction->toXml());
    }

    if (pEventScheme) delete pEventScheme;
    if (pAction) delete pAction;
    if (pEvthnd) delete pEvthnd;
    if (pActhnd) delete pActhnd;
  }
}

